package com.beep.erp.model.earlystage.standard;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.beep.erp.Constants;
import com.beep.erp.R;
import com.beep.erp.common.base.BaseBean;
import com.beep.erp.common.base.ToolbarActivity;
import com.beep.erp.common.bean.BizStorageLocationsListBean;
import com.beep.erp.common.bean.PalletGoodsBean;
import com.beep.erp.common.utils.MD5util;
import com.beep.erp.common.utils.SPUtils;
import com.beep.erp.common.utils.TimeUtils;
import com.beep.erp.common.utils.ToastUtils;
import com.beep.erp.common.utils.ToolsUtils;
import com.beep.erp.common.widget.ClearEditText;
import com.beep.erp.common.widget.popwindow.SelectDatePop;
import com.beep.erp.common.widget.popwindow.SelectOneTagPopupWindow;
import com.beep.erp.model.transfer.pallet.PalletGoodsContract;
import com.beep.erp.model.transfer.pallet.PalletGoodsModel;
import com.beep.erp.model.transfer.pallet.PalletGoodsPresenter;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.beep.erp.AppContext._context;


public class StandardActivity extends ToolbarActivity<PalletGoodsPresenter, PalletGoodsModel> implements PalletGoodsContract.View {

    private static final String STATUS = "status";

    @BindView(R.id.et_product_code)
    ClearEditText etProductCode;
    @BindView(R.id.et_product_num)
    ClearEditText etProductNum;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.line2)
    View line2;
    @BindView(R.id.tv_product_date)
    TextView tvProductDate;
    @BindView(R.id.ll_product_date)
    LinearLayout llProductDate;
    @BindView(R.id.line1)
    View line1;
    @BindView(R.id.tv_select_location)
    TextView tvSelectLocation;
    @BindView(R.id.ll_select_location)
    LinearLayout llSelectLocation;
    @BindView(R.id.line3)
    View line3;
    private String userId;
    private String status;
    private ArrayList<BizStorageLocationsListBean.BizStorageLocationsBean> mLocationBeen = new ArrayList<>();
    private String locationId = "";


    public static void start(Context context, String status) {
        Intent intent = new Intent(context, StandardActivity.class);
        intent.putExtra(STATUS, status);
        context.startActivity(intent);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_stadard;
    }

    @Override
    protected boolean isBack() {
        return true;
    }

    @Override
    protected void initView() {
        getTvTitle().setText("期出期入");
        tvRight.setText(R.string.save);
        tvRight.setVisibility(View.VISIBLE);
        SPUtils spUtils = new SPUtils(_context, Constants.EXTRA_LOGIN_SP);
        userId = spUtils.getString(Constants.USER_ID);
        status = getIntent().getStringExtra(STATUS);
        if (status.endsWith("32")) {
            llProductDate.setVisibility(View.VISIBLE);
            line1.setVisibility(View.VISIBLE);
            llSelectLocation.setVisibility(View.VISIBLE);
            line3.setVisibility(View.VISIBLE);
        }
    }

    @OnClick({R.id.tv_right, R.id.ll_product_date, R.id.ll_select_location})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_right:
                doEarly();
                break;
            case R.id.ll_product_date:
                hideSoftKeyboard();
                SelectDatePop selectDatePop = new SelectDatePop(this, tvProductDate, "");
                selectDatePop.setOnClickOK((year, month, day) -> {
                    tvProductDate.setText(year + "年" + month + "月" + day + "日");
                });
                break;
            case R.id.ll_select_location:
                hideSoftKeyboard();
                SelectOneTagPopupWindow selectOneTagPopupWindow = new SelectOneTagPopupWindow(
                        this, tvSelectLocation, "", mLocationBeen, false
                );
                selectOneTagPopupWindow.setOnItemClcik((id, msg) -> {
                    locationId = id;
                    tvSelectLocation.setText(msg);
                });
                break;
        }
    }

    private void doEarly() {
        String edNumber = etProductNum.getText().toString();
        String edCode = etProductCode.getText().toString();
        String date = tvProductDate.getText().toString();
        String location = tvSelectLocation.getText().toString();
        if (ToolsUtils.isEmpty(edCode)) {
            ToastUtils.showShort("请输入商品编号");
            return;
        }
        if (ToolsUtils.isEmpty(edNumber)) {
            ToastUtils.showShort("请输入商品数量");
            return;
        }

        if (status.equals("32")) {
            if (ToolsUtils.isEmpty(date)) {
                ToastUtils.showShort(getString(R.string.please_select_product_date));
                return;
            }

            if (ToolsUtils.isEmpty(location)) {
                ToastUtils.showShort(getString(R.string.please_select_location));
                return;
            }
        }

        long mills = TimeUtils.getCurTimeMills();
        Map<String, String> map = new HashMap<>();
        map.put("userId", userId);
        map.put("barcode", "1");
        map.put("number", edNumber);
        map.put("goodsCode", edCode);
        map.put("typeInfo", status);
        try {
            map.put("manufactureDate", URLEncoder.encode(date, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        map.put("storageLocationId", locationId);

        mPresenter.saveEarlyStoge(MD5util.MD5Encode(ToolsUtils.buildSignStr(map, mills)), mills + "", "1", edCode, edNumber, status, userId, date, locationId);
    }

    @Override
    public void showLoading() {
        showWaitDialog(getString(R.string.optoning));
    }

    @Override
    public void hideLoading() {
        hideWaitDialog();
    }

    @Override
    public void showMessage(String message) {
        ToastUtils.showShort(message);
    }

    @Override
    public void getDate() {
        getBizStorageLocationList();
    }

    private void getBizStorageLocationList() {
        long mills = TimeUtils.getCurTimeMills();

        Map<String, String> map = new HashMap<>();
        map.put("type", "0");
        mPresenter.getBizStorageLocationList(MD5util.MD5Encode(ToolsUtils.buildSignStr(map, mills)), mills + "", "0");
    }

    @Override
    public void getPalletGoodsDetail(PalletGoodsBean palletGoodsBean) {

    }

    @Override
    public void movePosition(BaseBean baseBean) {

    }

    @Override
    public void saveEarlyStoge(BaseBean baseBean) {
        ToastUtils.showShort(getString(R.string.option_success));
        finish();
    }

    @Override
    public void getBizStorageLocationList(BizStorageLocationsListBean bizStorageLocationsListBean) {
        mLocationBeen.addAll(bizStorageLocationsListBean.getBizStorageLocations());
    }
}
